package cn.tansanqinger.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import cn.tansanqinger.pojo.Admin;
import cn.tansanqinger.service.impl.LoginServiceImpl;
import cn.tansanqinger.util.PageData;

@Controller
public class LoginController{
	
	@Autowired
	private LoginServiceImpl loginServiceImpl;
	
	@RequestMapping("/")
	public String index() {
		return "login";
	}
	
	/**
	 * 得到PageData
	 */
	public PageData getPageData(){
		return new PageData(this.getRequest());
	}
	
	/**
	 * 得到request对象
	 */
	public HttpServletRequest getRequest() {
		HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
		
		return request;
	}
	
	/**
	 * 退出账号
	 * @param request
	 */
	@RequestMapping("/exit")
	public void exit(HttpServletRequest request) {
		request.getSession().removeAttribute("admin");
	}
	
	@RequestMapping(value="/login_login")
	@ResponseBody
	public Object login(HttpServletRequest request)throws Exception{
		PageData pd = this.getPageData();
		String KEYDATA[] = pd.getString("KEYDATA").replaceAll("tansanqinger", "").replaceAll("TANSANQINGER", "").split(",C3H2,");
		//账号验证
		HttpSession session = request.getSession();
		Admin admin = new Admin();
		admin.setAccount(KEYDATA[0]);
		admin.setPasswd(KEYDATA[1]);
		admin.setAtime(KEYDATA[2]);
		return loginServiceImpl.AccountPasswdIsConsistent(request, KEYDATA[0], KEYDATA[1]);
	}
	
}
